<template>
  <div class="add">
    <el-dialog
      :title="{ add: '添加权限', edit: '编辑权限' }[mode]"
      :visible.sync="isShow"
      width="50%"
      @close="closeEvent"
    >
      <el-form ref="form" label-width="120px" :model="form" :rules="rules">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="form.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="form.description" />
        </el-form-item>
        <el-form-item label="企业可见" prop="enVisible">
          <el-switch
            v-model="form.enVisible"
            active-text="可见"
            inactive-text="不可见"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeEvent">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { sysPermissionPost, sysPermissionPut } from '@/api/permission'
export default {
  name: '',
  data() {
    return {
      isShow: false,
      // 显示编辑或者新增
      mode: 'add',
      form: {
        name: '', //	string	非必须
        code: '', //	string	非必须
        description: '', //	string	非必须
        enVisible: '0', //	string	非必须 值为0或者1
        type: 1, //	number	非必须 1表示添加页面 2表示添加按钮
        pid: '0', //	string	非必须 父级的id
        id: ''
      },
      rules: {
        name: [{ required: true, message: '必填', trigger: 'change' }],
        code: [{ required: true, message: '必填', trigger: 'change' }],
        description: [{ required: true, message: '必填', trigger: 'change' }],
        enVisible: [{ required: true, message: '必填', trigger: 'change' }]
      }
    }
  },
  computed: {},

  methods: {
    // 点击取消
    closeEvent() {
      this.isShow = false
      this.$refs.form.resetFields()
      this.form = {
        name: '', //	string	非必须
        code: '', //	string	非必须
        description: '', //	string	非必须
        enVisible: '0', //	string	非必须
        type: '', //	number	非必须
        pid: '' //	string	非必须
      }
    },
    // 点击确定 预校验表单 发请求 调接口
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          if (this.mode === 'add') {
            // 添加权限
            await sysPermissionPost(this.form)
            this.$message.success('添加成功')
          } else if (this.mode === 'edit') {
            // 修改权限
            await sysPermissionPut(this.form)
            this.$message.success('修改成功')
          }
          // 触发父级方法 调用接口 渲染页面
          this.$emit('getData')
          // 还原数据
          this.closeEvent()
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped></style>
